<!DOCTYPE html>
<html>
<head>
  <title>Tests pivot functionality in virtual cursors</title>
  <meta charset="utf-8" />
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />

  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js">
  </script>
  <script type="application/javascript"
          src="chrome://mochikit/content/chrome-harness.js">
  </script>

  <script type="application/javascript" src="../common.js"></script>
  <script type="application/javascript" src="../browser.js"></script>
  <script type="application/javascript" src="../events.js"></script>
  <script type="application/javascript" src="../role.js"></script>
  <script type="application/javascript" src="../states.js"></script>
  <script type="application/javascript" src="../pivot.js"></script>
  <script type="application/javascript" src="../layout.js"></script>

  <script type="application/javascript">
    var gBrowserWnd = null;
    var gQueue = null;

    function doTest()
    {
      var rootAcc = getAccessible(browserDocument(), [nsIAccessibleDocument]);
      ok(rootAcc.virtualCursor,
         "root document does not have virtualCursor");

      var doc = currentTabDocument();
      var docAcc = getAccessible(doc, [nsIAccessibleDocument]);

      // Test that embedded documents have their own virtual cursor.
      is(docAcc.childDocumentCount, 1, "Expecting one child document");
      ok(docAcc.getChildDocumentAt(0).virtualCursor,
         "child document does not have virtualCursor");

      gQueue = new eventQueue();

      gQueue.onFinish = function onFinish()
      {
        closeBrowserWindow();
      }

      queueTraversalSequence(gQueue, docAcc, HeadersTraversalRule, null,
                             ['heading-1-1', 'heading-2-1', 'heading-2-2']);

      queueTraversalSequence(
        gQueue, docAcc, ObjectTraversalRule, null,
        ['Main Title', 'Lorem ipsum ',
         'dolor', ' sit amet. Integer vitae urna leo, id ',
         'semper', ' nulla. ', 'Second Section Title',
         'Sed accumsan luctus lacus, vitae mollis arcu tristique vulputate.',
         'An ', 'embedded', ' document.', 'Hide me', 'Link 1', 'Link 2',
         'Link 3', 'Hello', 'World']);

      // Just a random smoke test to see if our setTextRange works.
      gQueue.push(
        new setVCRangeInvoker(
          docAcc,
          getAccessible(doc.getElementById('paragraph-2'), nsIAccessibleText),
          [2,6]));

      gQueue.push(new removeVCPositionInvoker(
        docAcc, doc.getElementById('hide-me')));

      gQueue.push(new removeVCRootInvoker(
        doc.getElementById('links')));

      var [x, y] = getBounds(getAccessible(doc.getElementById('heading-1-1')));
      gQueue.push(new moveVCCoordInvoker(docAcc, x + 1, y + 1, true,
                                         HeadersTraversalRule, 'heading-1-1'));

      // Already on the point, so we should not get a move event.
      gQueue.push(new moveVCCoordInvoker(docAcc, x + 1, y + 1, true,
                                         HeadersTraversalRule, false));

      // Attempting a coordinate outside any header, should not move.
      gQueue.push(new moveVCCoordInvoker(docAcc, x - 1, y - 1, true,
                                         HeadersTraversalRule, false));

      // Attempting a coordinate outside any header, should move to null
      gQueue.push(new moveVCCoordInvoker(docAcc, x - 1, y - 1, false,
                                         HeadersTraversalRule, null));

      queueTraversalSequence(
        gQueue, docAcc, ObjectTraversalRule,
        getAccessible(doc.getElementById('paragraph-1')),
        ['Lorem ipsum ', 'dolor', ' sit amet. Integer vitae urna leo, id ',
         'semper', ' nulla. ']);

      gQueue.push(new setModalRootInvoker(docAcc, docAcc.parent,
                                          NS_ERROR_INVALID_ARG));

      // Put cursor in an ignored subtree
      // set isFromUserInput to false, just to test..
      gQueue.push(new setVCPosInvoker(docAcc, null, null,
                                      getAccessible(doc.getElementById("hidden-link")),
                                      false));
      // Next item shoud be outside of that subtree
      gQueue.push(new setVCPosInvoker(docAcc, "moveNext", ObjectTraversalRule, "An "));

      gQueue.invoke();
    }

    SimpleTest.waitForExplicitFinish();
    addLoadEvent(function () {
      /* We open a new browser because we need to test with a top-level content
         document. */
      openBrowserWindow(
        doTest,
        getRootDirectory(window.location.href) + "doc_virtualcursor.html");
    });
  </script>
</head>
<body id="body">

  <a target="_blank"
     title="Introduce virtual cursor/soft focus functionality to a11y API"
     href="https://bugzilla.mozilla.org/show_bug.cgi?id=698823">Mozilla Bug 698823</a>
  <p id="display"></p>
  <div id="content" style="display: none"></div>
  <pre id="test">
  </pre>
</body>
</html>
